<?php

declare(ENCODING = 'UTF-8');

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *	
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */	

/**
 * @package auxo
 * @subpackage Web
 * @version $Id$
 */

/**	
 * The Session Class
 *	
 * @package auxo
 * @subpackage Web
 * @version $Id$	
 * @copyright Copyright belongs to the respective authors
 * @author AHN
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
class tx_auxo_Session {
	
	/**
	 * A flag that indicates if a sessions has been started
	 *
	 * @var unknown_type
	 */
	private $started = false;
	
	/**
	 * Sets the session cache expire time in minutes
	 *
	 * @param int $minutes
	 * @throws tx_auxo_SessionAlreadyStartedException thrown if a session has been already started
	 */
	public function setCacheExpire($minutes) {
		if ($this->started) {
			throw new tx_auxo_SessionAlreadyStartedException();
		}
		session_cache_expire($minutes);		
	}
	
	/**
	 * Starts a new session
	 * 
	 * @throws tx_auxo_SessionAlreadyStartedException thrown if a session has been already started
	 */
	public function start() {
		if ($this->started) {
			throw new tx_auxo_SessionAlreadyStartedException();
		}
		session_start();
		$this->started = true;
	}
	
	/**
	 * Returns true if a sessions has been started otherwise false
	 *
	 * @return boolean $started true if sessions has been started
	 */
	public function isStarted() {
		return $this->started;
	}
	
	/**
	 * Registers a variable at the current session
	 *
	 * @param string $name
	 * @param mixed $value
	 * @throws tx_auxo_SessionNotStartedException if no session has been started
	 * @throws tx_auxo_SessionRegisterException thrown if data could not be registered
	 */
	public function register($name, $value) {
		if ($this->started) {
			throw new tx_auxo_SessionNotStartedException();
		}
		if (is_resource($value)) {
			throw new tx_auxo_SessionRegisterException(sprintf('Resource %s can not be registered at sessions', $value));
		}
		$_SESSION[$name] = $value;
	}
	
	/**
	 * Unregisters a variable a the current session
	 *
	 * @param string $name
	 * @throws tx_auxo_SessionNotStartedException if no session has been started
	 */
	public function unregister($name) {
		if ($this->started) {
			throw new tx_auxo_SessionNotStartedException();
		}
		unset($_SESSION[$name]);
	}
	
	/**
	 * Returns true if a variable has been registered with this session
	 *
	 * @param string $name name of variable
	 * @return boolean $registered true if registered otherwise false
	 * @throws tx_auxo_SessionNotStartedException if no session has been started
	 */
	public function isRegistered($name) {
		if ($this->started) {
			throw new tx_auxo_SessionNotStartedException();
		}
		return isset($_SESSION[$name]);
	}
	
	/**
	 * Returns the current session Id
	 *
	 * @return string $id session id
	 * @throws tx_auxo_SessionNotStartedException if no session has been started
	 */
	public function getId() {
		if ($this->started) {
			throw new tx_auxo_SessionNotStartedException();
		}		
		return session_id();
	}
	
	/**
	 * Close the current Session
	 *
	 * @throws tx_auxo_SessionNotStartedException if no session has been started
	 */
	public function close() {
		if (! $this->started) {
			throw new tx_auxo_SessionNotStartedException();
		}
		session_write_close();
		$this->started = false;
	}
	
	/**
	 * Cleans all variables that have been registered in this session
	 *
	 * @throws tx_auxo_SessionNotStartedException if no session has been started
	 */
	public function cleanup() {
		if (! $this->started) {
			throw new tx_auxo_SessionNotStartedException();
		}
		session_unset();
	}
	
	/**
	 * Destroys this session
	 *
	 * @throws tx_auxo_SessionNotStartedException if no session has been started
	 */
	public function destroy() {
		if (! $this->started) {
			throw new tx_auxo_SessionNotStartedException();
		}
		session_destroy();	
		$this->started = false;	
	}
}
?>